package com.ig.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ig.model.Book;

/**
 * BookDAOImpl is the implementation class for DB operations
 * 
 * @author skodali
 * 
 */
public class BookDAOImpl implements BookDAO {

	protected BookDAOImpl() {

	}

	private static String GET_BOOK = "SELECT * FROM BOOK WHERE book_ean = ";
	private static String INSERT_BOOK = "INSERT INTO BOOK (book_ean, book_title, book_author, book_list_price) VALUES ";
	Connection con;
	Statement stmt;
	ResultSet rs;

	public Book getBookInfo(String ean) {
		Book book = null;
		try {
			rs = getStatement().executeQuery(GET_BOOK + "'" + ean + "'");
			book = populateBook(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				closeResources();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return book;
	}

	public boolean storeBookInfo(Book book) {
		try {
			getStatement().executeUpdate(
					INSERT_BOOK + "('" + book.getEan() + "', '"
							+ checkQuotation(book.getTitle()) + "', '"
							+ book.getAuthor() + "', '" + book.getListPrice()
							+ "')");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.err.println(e.getMessage());
			e.printStackTrace();
			return false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				closeResources();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

	public Statement getStatement() throws ClassNotFoundException, SQLException {
		//con = DBManager.getDB2Connection(); // DB 2 Connection
		con = DBManager.getMySQLConnection(); // My SQL Connection
		stmt = con.createStatement();
		return stmt;
	}

	public void closeResources() {
		try {
			rs.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Book populateBook(ResultSet rs) throws SQLException {
		Book book = null;
		if (rs.next()) {
			book = new Book();
			book.setEan(rs.getString("book_ean"));
			book.setAuthor(makeSingleTypedList(rs.getString("book_author")));
			book.setTitle(rs.getString("book_title"));
			book.setListPrice(rs.getString("book_list_price"));
		}
		return book;
	}

	/** Builds a list containing the single typed object specified. */
	public static <Type> List<Type> makeSingleTypedList(Type object) {
		List<Type> list = new ArrayList<Type>();
		list.add(object);
		return list;
	}

	private static String checkQuotation(String str) {
		if (str.contains("'")) {
			return str.replace("'", "''");
		}
		return str;

	}
}
